package net.alpcontrol.bi.colligation.controller;


import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import net.alpcontrol.bi.colligation.service.ICompanyService;
import net.alpcontrol.bi.colligation.entity.Company;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.alpcontrol.bi.bootstrap.bean.Message;
import java.util.List;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 丛庆
 * @since 2020-08-31
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(tags = {""})
@RestController
@RequestMapping("/company")
public class CompanyController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ICompanyService companyService;

    /**
     * 查询分页数据
     */
    @ApiOperation(value = "查询分页数据")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Message<IPage<Company>> findListByPage(@RequestParam(name = "pageNum", defaultValue = "1") int pageNum,@RequestParam(name = "pageSize", defaultValue = "20") int pageSize){
        Page<Company> page = new Page<>(pageNum, pageSize);
        IPage<Company> result = companyService.selectPage(page);
        return Message.success("查询成功",result);
    }

    /**
     * 查询所有公司数据
     */
    @ApiOperation(value = "查询所有公司数据")
    @RequestMapping(value = "/companyList", method = RequestMethod.GET)
    public Message<List<Company>> findList(){
        return Message.success("查询成功",companyService.selectList());
    }

    /**
     * 根据id查询
     */
    @ApiOperation(value = "根据id查询数据")
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    public Message<Company> getById(@RequestParam("id") String id){
        return Message.success("查询成功",companyService.getById(id));
    }

    /**
     * 新增
     */
    @ApiOperation(value = "新增数据")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Message add(@RequestBody Company company){
        companyService.save(company);
        return Message.success("新增成功");
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除数据")
    @RequestMapping(value = "/del", method = RequestMethod.DELETE)
    public Message delete(@RequestParam("ids") List<String> ids){
        companyService.removeByIds(ids);
        return Message.success("删除成功");
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新数据")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Message update(@RequestBody Company company){
        companyService.saveOrUpdate(company);
        return Message.success("修改成功");
    }
}
